package spring.module.event;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 事件发布
 * 
 * @author Lucky Yang

 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderService {

    /** 注入ApplicationContext用来发布事件 */
    private final ApplicationContext applicationContext;

    /**
     * 下单
     *
     * @param orderId 订单ID
     */
    public String buyOrder(String orderId) {
        long start = System.currentTimeMillis();
        // 1.查询订单详情

        log.info("任务开始 {}", orderId);
        // 2.检验订单价格
        applicationContext.publishEvent(new OrderProductEvent(this, orderId));

        long end = System.currentTimeMillis();
        log.info("任务全部完成, 总耗时{}毫秒", end - start);
        return "购买成功";
    }
}
